package org.example.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.mapper.DApartmentMapper;
import org.example.mapper.DStaffMapper;
import org.example.pojo.DAmount;
import org.example.pojo.View.AmountView;
import org.example.pojo.excel.AmountDownload;
import org.example.pojo.select.DAmountSelect;
import org.example.service.DAmountService;
import org.example.mapper.DAmountMapper;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
* @author chenhongkai
* @description 针对表【d_amount(宿舍水电费表)】的数据库操作Service实现
* @createDate 2024-06-06 09:17:36
*/
@Service
public class DAmountServiceImpl extends ServiceImpl<DAmountMapper, DAmount>
    implements DAmountService{

    @Autowired
    private DAmountMapper dAmountMapper;

    @Autowired
    private DStaffMapper dStaffMapper;

    @Autowired
    private DApartmentMapper dApartmentMapper;

    @Override
    public Result<Map> selectAll(DAmountSelect dAmountSelect) {
        //分页查询员工
        Page<DAmountSelect> page = new Page<>(dAmountSelect.getCurrentPage(),dAmountSelect.getPageSize());
        page.setSearchCount(false);
        page.setTotal( dAmountMapper.SelectAllAmounts_total(dAmountSelect) );
        IPage<DAmountSelect> mapIPage = dAmountMapper.SelectAllAmounts(page,dAmountSelect);

        Map AmountMap = new HashMap<>();
        AmountMap.put("pageSize",mapIPage.getSize());
        AmountMap.put("pageTotal",mapIPage.getTotal());
        AmountMap.put("amountData",mapIPage.getRecords());
        return Result.ok(AmountMap);
    }

    @Override
    public Result<Map> SelectAmountByApartmentId(DAmountSelect dAmountSelect) {
        List<DAmountSelect> dAmountSelects = dAmountMapper.SelectAmountByApartmentId(dAmountSelect);
        Map AmountMap = new HashMap<>();
        AmountMap.put("staffs",dAmountSelects);
        return Result.ok(AmountMap);
    }


//    @Override
//    public Result<Map> AddDAmount(DAmount dAmountIn) {
//        //按宿舍结算水电订单
//        List<DApartment> dApartments = dApartmentMapper.selectList(null);
//        DAmount dAmount = new DAmount();
//        for (DApartment dApartment : dApartments){
//            LambdaQueryWrapper<DAmount> amountLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            amountLambdaQueryWrapper.eq(DAmount::getApartmentId,dApartment.getApartmentId())
//                                    .eq(DAmount::getAmountDate,dAmount.getAmountDate());
//            DAmount selectAmount = dAmountMapper.selectOne(amountLambdaQueryWrapper);
//            if(selectAmount == null ){
//                dAmount.setAmountDate(dAmountIn.getAmountDate());
//                dAmount.setApartmentId(dApartment.getApartmentId());
//                dAmount.setAmountStatus(0);
//                dAmountMapper.InsertStaffAmount(dAmount);
//            }
//        }
//        return Result.ok(null);
//    }

    @Override
    public Result<List<String>> SelectAmountDate() {
        List<String> AmountDateMap = dAmountMapper.selectAmountDate();
        return Result.ok(AmountDateMap);
    }

//    @Override
//    public Result<Map> DeleteAmountDate(DAmount dAmount) {
//        LambdaQueryWrapper<DAmount> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(DAmount::getApartmentId,dAmount.getApartmentId())
//                        .eq(DAmount::getAmountDate,dAmount.getAmountDate());
//        dAmountMapper.delete(lambdaQueryWrapper);
//        return Result.ok(null);
//    }

//    @Override
//    public Result<Map> SubmitAmount(DAmount dAmount) {
//        LambdaQueryWrapper<DAmount> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(DAmount::getApartmentId,dAmount.getApartmentId())
//                .eq(DAmount::getAmountDate,dAmount.getAmountDate());
//        List<DAmount> dAmountList = dAmountMapper.selectList(lambdaQueryWrapper);
//        int staffNumber = dAmountList.size();
//        double avgElectricAmount = dAmount.getAmountElectric() / staffNumber;
//        double avgHydroAmount = dAmount.getAmountHydro() / staffNumber;
//        double avgHydro = dAmount.getHydro() / staffNumber;
//        double avgElectric = dAmount.getElectric() / staffNumber;
//
//        DAmount dAmount1 = new DAmount();
//        dAmount1.setApartmentId(dAmount.getApartmentId());
//        dAmount1.setAmountElectric(avgElectricAmount);
//        dAmount1.setAmountHydro(avgHydroAmount);
//        dAmount1.setAmountDate(dAmount.getAmountDate());
//        dAmount1.setElectric(avgElectric);
//        dAmount1.setHydro(avgHydro);
//
//        dAmountMapper.UpdateAmount(dAmount1);
//
//        return Result.ok(null);
//
//    }

    @Override
    public Result<Map> DownloadAmount(DAmountSelect dAmountSelect) {
        List<AmountDownload> amountDownloads = dAmountMapper.selectDownloadAmount(dAmountSelect.getMonth());
        Map result = new HashMap();
        result.put("amountDownloads",amountDownloads);
        return Result.ok(result);
    }


    @Override
    public Result<Map> GetEncryptionAmount(String month) {
        List<AmountView> dAmountList = dAmountMapper.GetEncryptionAmount(month);
        Map amountMap = new HashMap();
        amountMap.put("total",dAmountList.size());
        amountMap.put("bill",dAmountList);
        return Result.ok(amountMap);
    }

}




